export function isFunction(fn){
    return typeof fn === 'function';
}

export function getType(Obj){
    return typeof Obj;
}

export function isArray(arr){
    if(typeof Array.isArray === 'function')
        return Array.isArray(arr);
    else {
        return Object.prototype.toString.apply(arr) === '[object Array]';
    }
}

/**
 * 格式化数据
 * @param val
 * @param type
 * @param defaultVal
 * @returns {string|*}
 */
export const getFormatted = (val, type, defaultVal = '-') => {
    if (isNaN(val)) return defaultVal;
    if (!type) return val;
    if (isFunction(type)) return type(val);
   return val;
};

export function get(obj, propertiesString) {
    if(typeof propertiesString !== 'string') {
        console.log('properties must be string');
        return null;
    }

    const properties = propertiesString.split('.');
    for(const property of properties){
        if(obj.hasOwnProperty(property)) {
            obj = obj.property;
        } else {
            return null;
        }
    }
    return obj;
}

export function set(obj, propertiesString, value) {
    if(typeof propertiesString !== 'string') {
        console.log('fields must be string');
        return null;
    }

    const properties = propertiesString.split('.');
   for(let i = 0; i< properties.length; i++){
       if(i === properties.length - 1 || !obj.hasOwnProperty(properties[i])) {
           obj[i] = value;
           break;
       }else {
           obj = obj.properties[i];
       }
   }
    return obj;
}
